<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack()">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <UploadImg @uploadImg="getUploadImg" :editImg="editImg" :imgTitle="imgTitle"></UploadImg>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
              <el-input v-model="ruleForm.company"></el-input>
            </el-form-item>
            <el-form-item label="职务" prop="position">
              <el-input v-model="ruleForm.position"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="idcard">
              <el-input v-model="ruleForm.idcard"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email" type="email"></el-input>
            </el-form-item>
            <el-form-item label="地区" prop="area">
              <el-input v-model="ruleForm.area"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="ruleForm.address"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from "../public/UploadImg";
import checkPhone from "../common/checkPhone";

export default {
  name: "editcontact",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        company: [{ required: true, message: "公司不能为空", trigger: "blur" }],
        position: [ { required: true, message: "职务不能为空", trigger: "blur" }],
        phone: [{ required: false, validator: checkPhone, trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        phone: "",
        company: "",
        position: "",
        email:"",
        avatar: "",
        idcard:"",
        state: "",
        projectId: "",
        area:"",
        address:"",
        projectId:localStorage.getItem("project_Id")
      },
      editImg: "",
      contactId: "",
      imgTitle: "上传头像"
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
  },
  watch: {
    $route: "getPath",
    editData: function() {
      const item = this.editData;
      this.ruleForm = this.editData;
      this.contactId = item.contactId;
      this.editImg = item.avatar;
    }
  },
  methods: {
    saveClick(ruleForm) { 
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.contactId == "" || this.contactId == undefined) {
            this.$axios.post("/Contact/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/Contact/" + this.contactId + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.contactId = "";
      this.$refs.ruleForm.resetFields();
    },
    getUploadImg(imgSrc) {
      this.ruleForm.avatar = imgSrc;
    }
  },
  components: {
    UploadImg
  }
};
</script>